﻿using iTextSharp.text.pdf.parser;
using System;
using System.IO;
using System.Windows.Forms;

namespace PdfOperator
{
    public partial class FrmPdfToTxt : Form
    {
        public FrmPdfToTxt()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 选择待转换文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenfile_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Title = "打开PDF";
            fileDialog.Filter = "pdf文件|*.pdf";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string name = fileDialog.FileName;
                txtFilePath.Text = name;
            }
        }

        /// <summary>
        /// 选择保存路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSavePath_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFilePath.Text))
            {
                MessageBox.Show("请先选择要操作的pdf！");
                return;
            }
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Title = "选择保存路径";
            saveFile.Filter = "txt文件|*.txt";
            saveFile.DefaultExt = ".txt";
            saveFile.FileName = "pdf转为txt的文件.txt";
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                txtSavePath.Text = saveFile.FileName;
            }
        }


        /// <summary>
        /// 开始转换为txt
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFilePath.Text))
            {
                MessageBox.Show("请选择要操作的pdf文件！");
                return;
            }

            var pdfReader = new iTextSharp.text.pdf.PdfReader(txtFilePath.Text);
            StreamWriter output = new StreamWriter(new FileStream(txtSavePath.Text, FileMode.Create));
            int pageCount = pdfReader.NumberOfPages;
            for (int pg = 1; pg <= pageCount; pg++)
            {
                ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
                var value = PdfTextExtractor.GetTextFromPage(pdfReader, pg, strategy);
                value = value.Replace(" ", "");
                //Console.WriteLine(value);
                output.Write(value);
            }

            output.Flush();
            output.Close();

            MessageBox.Show("完成");
        }

        private void btnOpenWord_Click(object sender, EventArgs e)
        {
            //判断文件是否存在
            if (!System.IO.File.Exists(txtSavePath.Text))
            {
                return;
            }
            else
            {
                //存在文件
                System.Diagnostics.Process.Start(txtSavePath.Text);
            }
        }

        /// <summary>
        /// 开始拖放文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtFilePath_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
                this.txtFilePath.Cursor = Cursors.Arrow; //指定鼠标形状   
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        /// <summary>
        /// 接收拖放文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtFilePath_DragDrop(object sender, DragEventArgs e)
        {
            string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            if (System.IO.Path.GetExtension(path).ToLower() == ".pdf")
            {
                this.txtFilePath.Text = path;
            }
            this.txtFilePath.Cursor = Cursors.IBeam; //还原鼠标形状 
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
